import {
  getWxNewsDetail,
  getWxNewsList,
  getWxNewsList2,
  getWxNewsList3,
  setWxNews,
  syncWxNews,
  getContentTypes,
  addContentType,
  setContentType,
  delContentType,
  addWxSendPlan,
  getWxSendPlans,
  setWxSendPlanStatus,
  getWxCheckList,
  setReleaseCheck,
  getReleaseCheckNum,
  delWxNews,
  getSmsNewsList,
  editSms,
  getSmsDetail,
  getSmsCheckList,
  getMailNewsList,
  editMail,
  getMailDetail,
  getMailCheckList,
  getCurMailplt,
  topContent,
  delContent,
  getCaptcha,
  testSend,
  sendRecord,
  sendRecordDownLoad,
  newSendPlan,
  setShortLink,
  getWePageList,
  getWePageList2,
  saveWePage,
  getWePageDetail,
  delWePages,
  moveWepages,
  publicWepage,
  previewWepage,
  wePageAuditList,
  setWePageWxInfo,
  getWePageWxInfo,
  addWePageSendPlan,
  getWePageSendPlan,
  getFormList,
  delFormCheck,
  delFormList,
  getFormDetail,
  getFormDetail2,
  saveForm,
  viewForm,
  publishForm,
  getWXcardList,
  getWXpayAccountList,
  getWXvalidAccountList,
  addPoster,
  getPosterList,
  updatePoster,
  delPoster,
  getPoster,
  setPosterAttr,
  delMultPoster,
  getPosterCheckList
} from '@/service/getData'
import * as type from '@/store/mutation-types'

const state = {
  wxNewsList: { // 微信图文列表
    page: {},
    list: []
  },
  wxNewsDetail: { // 微信图文详情
    groupType: 0,
    commonType: 0,
    attributes: null,
    articles: [{showCoverPic: '0', index: 0}]
  },
  wxSendInfo: { // 发送设置
    sendType: null,
    sendTime: null,
    conditions: [{appid: null, tagId: null}]
  },
  wxSendList: { // 发送记录
    page: {},
    list: []
  },
  wxCheckList: { // 审核列表
    page: {},
    list: []
  },
  contentType: [], // 内容性质
  // 短信内容列表
  smsNewsList: {
    page: {}, // 列表
    list: [] // 分页
  },
  smsDetail: {},
  smsViewCont: '',  //  短信预览
  smsCheckList: { // 短信审核列表
    page: {
      total: 0
    },
    list: []
  },
  // 邮件内容列表
  mailNewsList: { // 列表
    page: {
      total: 0
    },
    list: [] // 分页
  },
  mailDetail: {},
  mailCheckList: { // 邮件审核列表
    page: {},
    list: []
  },
  sendRecordList: [],
  clientAttrMap: {
    ssoid: 'ssoid',
    username: '用户名',
    realName: '真实姓名',
    email: '邮箱',
    mobile: '手机号',
    avatar: '头像',
    city: '所在地',
    birthday: '生日',
    maritalStatus: '婚姻状态',
    sex: '性别',
    year: '出生年份',
    edu: '教育程度',
    source: '粉丝来源',
    registerType: '注册类型',
    registerTime: '注册时间',
    Integral: '积分',
    growthValue: '成长值',
    level: '等级',
    freeze: '是否冻结'
  },
  // 表单列表
  formList: {
    page: {
      total: 0
    },
    list: []
  },
  formDetail: {},
  formHtmlMap: [

  ],
  payData: [],
  WXcardList: [],
  wxPayAccList: [],
  wxValidAccList: [],
  // 微页面
  wePageList: {
    list: [],
    page: {}
  },
  wePageDetail: {}, // 微页面详情
  wePageCheckList: { // 微页面审核列表
    list: [],
    page: {}
  },
  wePageWxInfo: {},
  wePageSendPlan: {
    list: [],
    page: {}
  },
  // 海报
  posterList: {
    data: [],
    page: {}
  },
  // 海报审核列表
  posterCheckList: {
    data: [],
    page: {}
  },
  curPoster: {}
}
const actions = {
  // 微信内容相关
  async getWxNewsList({commit, state}, payload) {
    let res = await getWxNewsList(payload)
    commit(type.SET_WX_NEWS_LIST, res.data.data)
    return res
  },
  async getWxNewsList2({commit, state}, payload) {
    let res = await getWxNewsList2(payload)
    commit(type.SET_WX_NEWS_LIST, res.data.data)
    return res
  },
  async getWxNewsList3({commit, state}, payload) {
    let res = await getWxNewsList3(payload)
    commit(type.SET_WX_NEWS_LIST, res.data.data)
    return res
  },
  async getWxNewsDetail({commit, state}, id) {
    let res = await getWxNewsDetail(id)
    commit(type.SET_WX_NEWS, res.data.data)
    return res
  },
  async setWxNews({commit, state}, payload) {
    let res = await setWxNews(payload)
    return res
  },
  async delWxNews({commit, state}, payload) {
    let res = await delWxNews(payload)
    return res
  },
  async syncWxNews({commit, state}, appid) {
    let res = await syncWxNews(appid)
    return res
  },
  async addWxSendPlan({commit, state}, payload) {
    let res = await addWxSendPlan(payload)
    return res
  },
  async getWxSendPlans({commit, state}, payload) {
    let res = await getWxSendPlans(payload)
    if (res.data.data) {
      commit(type.SET_WX_SEND_PLANS, res.data.data)
    }
    return res
  },
  async setWxSendPlanStatus({commit, state}, payload) {
    let res = await setWxSendPlanStatus(payload)
    return res
  },
  async getContentTypes({commit, state}) {
    let res = await getContentTypes()
    commit(type.SET_CONTENT_TYPE, res.data.data)
    return res
  },
  async addContentType({commit, state}, payload) {
    let res = await addContentType(payload)
    return res
  },
  async setContentType({commit, state}, payload) {
    let res = await setContentType(payload)
    return res
  },
  async delContentType({commit, state}, attrId) {
    let res = await delContentType(attrId)
    return res
  },
  async getWxCheckList({commit, state}, payload) {
    let res = await getWxCheckList(payload)
    commit(type.SET_WX_CHECK_LIST, res.data.data)
    return res
  },
  async setReleaseCheck({commit, state}, payload) {
    let res = await setReleaseCheck(payload)
    return res
  },
  async getReleaseCheckNum({commit, state}, payload) {
    let res = await getReleaseCheckNum(payload)
    return res
  },
  // 短信列表
  async getSmsNewsList({commit, state}, payload) {
    let res = await getSmsNewsList(payload)
    commit(type.GET_SMS_NEWS_LIST, res.data.data)
  },
  async editSms({commit, state}, payload) {
    let res = await editSms(payload)
    return res
  },
  async getSmsDetail({commit, state}, id) {
    let res = await getSmsDetail(id)
    commit(type.GET_SMS_DETAIL, res.data.data)
    return res
  },
  async getSmsCheckList({commit, state}, payload) {
    let res = await getSmsCheckList(payload)
    commit(type.SET_SMS_CHECK_LIST, res.data.data)
    return res
  },
  // 邮件列表
  async getMailNewsList({commit, state}, payload) {
    let res = await getMailNewsList(payload)
    commit(type.GET_MAIL_NEWS_LIST, res.data.data)
  },
  async editMail({commit, state}, payload) {
    let res = await editMail(payload)
    return res
  },
  async getMailDetail({commit, state}, id) {
    let res = await getMailDetail(id)
    commit(type.GET_MAIL_DETAIL, res.data.data)
    return res
  },
  async getMailCheckList({commit, state}, payload) {
    let res = await getMailCheckList(payload)
    commit(type.SET_MAIL_CHECK_LIST, res.data.data)
    return res
  },
  async getCurMailplt({commit, state}) {
    let res = await getCurMailplt()
    return res
  },
  // 置顶内容
  async topContent({commit, state}, payload) {
    let res = await topContent(payload.id, payload.top)
    return res
  },
  // 删除内容
  async delContent({commit, state}, payload) {
    let res = await delContent(payload.id, payload.del)
    return res
  },
  // 图片验证码
  async getCaptcha({commit, state}, payload) {
    let res = await getCaptcha(payload)
    return res
  },
  // 测试发送
  async testSend({commit, state}, payload) {
    let res = await testSend(payload)
    return res
  },
  // 短信/邮件发送设置
  async newSendPlan({commit, state}, payload) {
    let res = await newSendPlan(payload)
    return res
  },
  // 发送记录
  async sendRecord({commit, state}, payload) {
    let res = await sendRecord(payload.id)
    commit(type.GET_SEND_RECORD, res.data.data)
    return res
  },
  // 发送记录下载
  async sendRecordDownLoad({commit, state}, payload) {
    console.log(payload)
    let res = await sendRecordDownLoad(payload.planId)
    return res
  },
  // 生成短链接
  async setShortLink({commit, state}, payload) {
    let res = await setShortLink(payload)
    return res
  },
  /* 微页面 */
  async getWePageList({commit, state}, payload) {
    let res = await getWePageList(payload)
    const promises = []
    for (let i = 0, length = res.data.data.list.length; i < length; i++) {
      const promise = previewWepage(res.data.data.list[i].id)
      promises.push(promise)
      const res2 = await promise
      res.data.data.list[i].qrCode = res2.data.data.qrCode
    }
    Promise.all(promises).then(() => {
      commit('SET_WEPAGE_LIST', res.data.data)
    })
    return res
  },
  async getWePageList2({commit, state}, payload) {
    let res = await getWePageList2(payload)
    const promises = []
    for (let i = 0, length = res.data.data.list.length; i < length; i++) {
      const promise = previewWepage(res.data.data.list[i].id)
      promises.push(promise)
      const res2 = await promise
      res.data.data.list[i].qrCode = res2.data.data.qrCode
    }
    Promise.all(promises).then(() => {
      commit('SET_WEPAGE_LIST', res.data.data)
    })
    return res
  },
  async previewWepage(obj, id) {
    const res = await previewWepage(id)
    return res
  },
  async saveWePage({commit, state}, payload) {
    let res = await saveWePage(payload)
    return res
  },
  async getWePageDetail({commit, state}, id) {
    let res = await getWePageDetail(id)
    commit('SET_WEPAGE_DETAIL', res.data.data)
    return res
  },
  async delWePages({commit, state}, ids) {
    let res = await delWePages(ids)
    return res
  },
  async moveWepages({commit, state}, payload) {
    let res = await moveWepages(payload)
    return res
  },
  async publicWepage({commit, state}, id) {
    let res = await publicWepage(id)
    return res
  },
  async wePageAuditList({commit, state}, payload) {
    let res = await wePageAuditList(payload)
    const promises = []
    for (let i = 0, length = res.data.data.list.length; i < length; i++) {
      const promise = previewWepage(res.data.data.list[i].logicId)
      promises.push(promise)
      const res2 = await promise
      res.data.data.list[i].qrCode = res2.data.data.qrCode
    }
    Promise.all(promises).then(() => {
      commit('SET_WEPAGE_AUDIT_LIST', res.data.data)
    })
    return res
  },
  async setWePageWxInfo({commit, state}, payload) {
    let res = await setWePageWxInfo(payload)
    return res
  },
  async getWePageWxInfo({commit, state}, payload) {
    let res = await getWePageWxInfo(payload)
    commit('SET_WEPAGE_WXINFO', res.data.data)
    return res
  },
  async addWePageSendPlan({commit, state}, payload) {
    let res = await addWePageSendPlan(payload)
    return res
  },
  async getWePageSendPlan({commit, state}, payload) {
    let res = await getWePageSendPlan(payload)
    commit('SET_WEPAGE_SENDPLAN', res.data.data)
    return res
  },
  // 表单
  async getFormList({commit, state}, payload) {
    let res = await getFormList(payload)
    commit('SET_FORM_LIST', res.data.data)
    return res
  },
  async delFormCheck({commit, state}, payload) {
    let res = await delFormCheck(payload)
    return res
  },
  async delFormList({commit, state}, payload) {
    let res = await delFormList(payload)
    return res
  },
  async getFormDetail({commit, state}, id) {
    let res = await getFormDetail(id)
    const detail = res.data.data || {}
    commit('SET_FORM_DETAIL', detail)
    return res
  },
  async getFormDetail2({commit, state}, id) {
    let res = await getFormDetail2(id)
    const detail = res.data.data || {}
    commit('SET_FORM_DETAIL', detail)
    return res
  },
  async saveForm({commit, state}, payload) {
    let res = await saveForm(payload)
    return res
  },
  async viewForm({commit, state}, payload) {
    let res = await viewForm(payload)
    return res
  },
  async publishForm({commit, state}, payload) {
    let res = await publishForm(payload)
    return res
  },
  async getWXcardList({commit, state}, payload) {
    let res = await getWXcardList(payload)
    commit('SET_WX_CARD_LIST', res.data.data)
    return res
  },
  async getWXpayAccountList({commit, state}, payload) {
    let res = await getWXpayAccountList(payload)
    commit('SET_WX_PAY_ACC_LIST', res.data.data)
    return res
  },
  async getWXvalidAccountList({commit, state}, payload) {
    let res = await getWXvalidAccountList(payload)
    commit('SET_WX_VALID_ACC_LIST', res.data.data)
    return res
  },
  /* 海报 */
  async addPoster({commit, state}, payload) {
    const res = await addPoster(payload)
    return res
  },
  async updatePoster({commit, state}, payload) {
    const res = await updatePoster(payload)
    return res
  },
  async getPoster({commit, state}, id) {
    const res = await getPoster(id)
    commit('SET_CUR_POSTER', res.data.data)
    return res
  },
  async delPoster({commit, state}, payload) {
    const res = await delPoster(payload)
    return res
  },
  async getPosterList({commit, state}, payload) {
    const res = await getPosterList(payload)
    commit('SET_POSTER_LIST', res.data.data)
    return res
  },
  async setPosterAttr({commit, state}, payload) {
    const res = await setPosterAttr(payload)
    return res
  },
  async delMultPoster({commit, state}, payload) {
    const res = await delMultPoster(payload)
    return res
  },
  async getPosterCheckList({commit, state}, payload) {
    const res = await getPosterCheckList(payload)
    commit('SET_POSTER_CHECK_LIST', res.data.data)
    return res
  }
}
const mutations = {
  [type.SET_WX_NEWS_DETAIL](state, payload) {
    state.wxNewsDetail = payload
  },
  [type.SET_WX_NEWS_LIST](state, payload) {
    state.wxNewsList = payload
  },
  [type.UPDATE_WX_NEWS](state, payload) {
    state.wxNewsDetail = {...state.wxNewsDetail, ...payload}
  },
  [type.SET_WX_NEWS](state, payload) {
    state.wxNewsDetail = payload
  },
  [type.INIT_WX_NEWS](state) {
    state.wxNewsDetail = {
      groupType: 0,
      commonType: 0,
      attributes: null,
      articles: [{showCoverPic: '0', index: 0}]
    }
  },
  [type.SET_CONTENT_TYPE](state, payload) {
    state.contentType = payload
  },
  [type.UPDATE_WX_SEND_INFO](state, payload) {
    state.wxSendInfo = {...state.wxSendInfo, ...payload}
  },
  [type.SET_WX_SEND_PLANS](state, payload) {
    state.wxSendList = payload
  },
  [type.SET_WX_CHECK_LIST](state, payload) {
    state.wxCheckList = payload
  },
  // 短信
  [type.GET_SMS_NEWS_LIST](state, payload) {
    if (payload.page) {
      state.smsNewsList = payload
    }
  },
  [type.GET_SMS_DETAIL](state, payload) {
    state.smsDetail = payload
  },
  [type.SET_SMS_CHECK_LIST](state, payload) {
    if (payload) {
      state.smsCheckList = payload
    }
  },
  [type.SET_SMS_VIEWCONT](state, payload) {
    state.smsViewCont = payload
  },
  // 邮件
  [type.GET_MAIL_NEWS_LIST](state, payload) {
    if (payload.page) {
      state.mailNewsList = payload
    }
  },
  [type.GET_MAIL_DETAIL](state, payload) {
    state.mailDetail = payload
  },
  [type.SET_MAIL_CHECK_LIST](state, payload) {
    state.mailCheckList = payload
  },
  [type.GET_SEND_RECORD](state, payload) {
    state.sendRecordList = payload
  },
  /* 微页面 */
  ['SET_WEPAGE_LIST'](state, payload) {
    state.wePageList = payload
  },
  ['SET_WEPAGE_DETAIL'](state, payload) {
    state.wePageDetail = payload
  },
  ['SET_WEPAGE_WXINFO'](state, payload) {
    state.wePageWxInfo = payload
  },
  ['SET_WEPAGE_SENDPLAN'](state, payload) {
    state.wePageSendPlan = payload
  },
  /* 表单 */
  ['SET_FORM_LIST'](state, payload) {
    state.formList = payload
  },
  ['SET_FORM_DETAIL'](state, payload) {
    state.formDetail = payload
    if (payload.node) {
      state.formHtmlMap = JSON.parse(payload.node).formNode
      state.payData = []
      state.formHtmlMap.map((elem) => {
        if (elem.type === 'pay') {
          state.payData.push(elem.payObj)
        }
      })
    }
  },
  ['SET_WEPAGE_AUDIT_LIST'](state, payload) {
    state.wePageCheckList = payload
  },
  [type.SET_FORM_HTML_MAP](state, payload) {
    state.formHtmlMap = payload
  },
  [type.SET_PAY_DATA](state, payload) {
    state.payData = payload
  },
  [type.SET_WX_CARD_LIST](state, payload) {
    state.WXcardList = payload
  },
  [type.SET_WX_PAY_ACC_LIST](state, payload) {
    state.wxPayAccList = payload
  },
  [type.SET_WX_VALID_ACC_LIST](state, payload) {
    state.wxValidAccList = payload
  },
  /* 海报 */
  ['SET_POSTER_LIST'](state, payload) {
    state.posterList = payload
  },
  ['SET_CUR_POSTER'](state, payload) {
    state.curPoster = payload
  },
  ['SET_POSTER_CHECK_LIST'](state, payload) {
    state.posterCheckList = payload
  }
}
export default {
  state,
  actions,
  mutations
}
